package org.wk.common.service;

import org.apache.commons.fileupload.disk.DiskFileItem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.wk.common.qiniu.UploadManagerFacade;
import org.wk.core.common.tool.GeneratorStringUtils;

import java.io.File;
import java.util.Map;


@Service("uploadService")
public class UploadServiceImpl implements UploadService{
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    public Map<String, String> doUpload(MultipartFile upfile, String contentType) throws Exception {

        String uploadFileName = upfile.getOriginalFilename();
        String fileExtName = uploadFileName.substring(uploadFileName.lastIndexOf("."));
        String newName = GeneratorStringUtils.uuidStr()+fileExtName;
//        CommonsMultipartFile commonsMultipartFile = (CommonsMultipartFile) multipartFile;
//        DiskFileItem diskFileItem = (DiskFileItem) commonsMultipartFile.getFileItem();
//
//        File file = diskFileItem.getStoreLocation();
//        StringBuffer targetFileName = new StringBuffer();
//        long size = diskFileItem.getSize();
//        targetFileName.append(GeneratorStringUtils.uuidStr());
//        String _name = diskFileItem.getName();
//        targetFileName.append(_name.substring(_name.lastIndexOf("."), _name.length()));
//        logger.info("targetFileName is [{}]", targetFileName.toString());
//        // 七牛云存储上传资源
        Map<String, String> resMap =  UploadManagerFacade.upload(upfile.getBytes(), contentType, newName,false);
        return resMap;
    }

    @Override
    public Map<String, String> uploadImageByUrl(String url, String contentType) throws Exception{
        // 七牛云存储上传资源
        Map<String, String> resMap =  UploadManagerFacade.upload(url, contentType);
        return resMap;
    }


}
